package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.util.MallPeripheryOrderConvert;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallPeripheryOrderDtoMapper;
import com.starlink.model.domain.MallPeripheryOrderDO;
import com.starlink.mapper.MallPeripheryOrderMapper;
import com.starlink.model.request.MallPeripheryOrderRequest;
import com.starlink.service.MallPeripheryOrderService;
import com.starlink.model.vo.MallPeripheryOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallPeripheryOrderServiceImpl extends ServiceImpl<MallPeripheryOrderMapper, MallPeripheryOrderDO> implements MallPeripheryOrderService {
    @Autowired
    private MallPeripheryOrderMapper mallPeripheryOrderMapper;

    @Override
    public MallPeripheryOrderVO getByKey(long id) {
        MallPeripheryOrderVO mallPeripheryOrderVO = MallPeripheryOrderDtoMapper.INSTANCE.do2VO(getById(id));
        return mallPeripheryOrderVO;
    }

    @Override
    public PageInfo<MallPeripheryOrderVO> pageQuery(Pageable pageable,MallPeripheryOrderRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallPeripheryOrderDO> list = mallPeripheryOrderMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallPeripheryOrderVO> mallPeripheryOrderVOList = MallPeripheryOrderConvert.buildMallPeripheryOrderVoList(list);
        return new PageInfo(mallPeripheryOrderVOList);
    }

    @Override
    public List<MallPeripheryOrderVO> listQuery(MallPeripheryOrderRequest request) {
        List<MallPeripheryOrderDO> list = mallPeripheryOrderMapper.selectDOList(MallPeripheryOrderDtoMapper.INSTANCE.request2DO(request));
        List<MallPeripheryOrderVO> mallPeripheryOrderVOS = MallPeripheryOrderConvert.buildMallPeripheryOrderVoList(list);
        return mallPeripheryOrderVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallPeripheryOrderRequest request) {
        save(MallPeripheryOrderDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallPeripheryOrderRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallPeripheryOrderDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallPeripheryOrderMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertDO(MallPeripheryOrderRequest request) {
        MallPeripheryOrderDO mallPeripheryOrder = MallPeripheryOrderDtoMapper.INSTANCE.request2DO(request);
        mallPeripheryOrderMapper.insertDO(mallPeripheryOrder);
        return mallPeripheryOrder.getId();
    }
}
